<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">//micro template based on John Resg code</span><span class="WHIT">
<span class='line'>  2</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">tmpl</span><span class="PUNC">(</span><span class="NAME">tmpl_id</span><span class="PUNC">,</span><span class="NAME">data</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>  3</span> </span><span class="WHIT">	</span><span class="KEYW">try</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>  4</span> </span><span class="WHIT">		</span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="WHIT"> </span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">tmpl.cache</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">'undefined'</span><span class="WHIT"> </span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>  5</span> </span><span class="WHIT">			</span><span class="NAME">tmpl.cache</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">Array</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>  6</span> </span><span class="WHIT">	    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>  7</span> </span><span class="WHIT">		</span><span class="KEYW">if</span><span class="PUNC">(</span><span class="WHIT"> </span><span class="NAME">tmpl.cache</span><span class="PUNC">[</span><span class="NAME">tmpl_id</span><span class="PUNC">]</span><span class="PUNC">!=</span><span class="KEYW">null</span><span class="WHIT"> </span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>  8</span> </span><span class="WHIT">			</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">fn</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">tmpl.cache</span><span class="PUNC">[</span><span class="NAME">tmpl_id</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>  9</span> </span><span class="WHIT">			</span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">fn</span><span class="PUNC">(</span><span class="NAME">data</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">		
<span class='line'> 10</span> 		</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 11</span> </span><span class="WHIT">		</span><span class="NAME">str</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">document.getElementById</span><span class="PUNC">(</span><span class="NAME">tmpl_id</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">innerHTML</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 12</span> </span><span class="WHIT">		</span><span class="NAME">str</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">str.replace</span><span class="PUNC">(</span><span class="REGX">/\\/g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">"@SPC@"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 13</span> </span><span class="WHIT">		</span><span class="NAME">str</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">str.replace</span><span class="PUNC">(</span><span class="REGX">/'/g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">"&apos;"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 14</span> </span><span class="WHIT">		</span><span class="NAME">fnStr</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">"var p=[],print=function(){p.push.apply(p,arguments);};"</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT">
<span class='line'> 15</span> </span><span class="WHIT">	    
<span class='line'> 16</span> 	    </span><span class="COMM">// Introduce the data as local variables using with(){}</span><span class="WHIT">
<span class='line'> 17</span> </span><span class="WHIT">	    </span><span class="STRN">"with(obj){p.push('"</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT">
<span class='line'> 18</span> </span><span class="WHIT">	    
<span class='line'> 19</span> 	    </span><span class="COMM">// Convert the template into pure JavaScript</span><span class="WHIT">
<span class='line'> 20</span> </span><span class="WHIT">	    </span><span class="NAME">str</span><span class="WHIT">
<span class='line'> 21</span> </span><span class="WHIT">	      </span><span class="PUNC">.</span><span class="NAME">replace</span><span class="PUNC">(</span><span class="REGX">/[\r\t\n]/g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">" "</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 22</span> </span><span class="WHIT">	      </span><span class="PUNC">.</span><span class="NAME">split</span><span class="PUNC">(</span><span class="STRN">"&lt;%"</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">join</span><span class="PUNC">(</span><span class="STRN">"\t"</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 23</span> </span><span class="WHIT">	      </span><span class="PUNC">.</span><span class="NAME">replace</span><span class="PUNC">(</span><span class="REGX">/((^|%>)[^\t]*)'/g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">"$1\r"</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 24</span> </span><span class="WHIT">	      </span><span class="PUNC">.</span><span class="NAME">replace</span><span class="PUNC">(</span><span class="REGX">/\t=(.*?)%>/g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">"',$1,'"</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 25</span> </span><span class="WHIT">	      </span><span class="PUNC">.</span><span class="NAME">split</span><span class="PUNC">(</span><span class="STRN">"\t"</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">join</span><span class="PUNC">(</span><span class="STRN">"');"</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 26</span> </span><span class="WHIT">	      </span><span class="PUNC">.</span><span class="NAME">split</span><span class="PUNC">(</span><span class="STRN">"%>"</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">join</span><span class="PUNC">(</span><span class="STRN">"p.push('"</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 27</span> </span><span class="WHIT">	      </span><span class="PUNC">.</span><span class="NAME">split</span><span class="PUNC">(</span><span class="STRN">"\r"</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">join</span><span class="PUNC">(</span><span class="STRN">"\\'"</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 28</span> </span><span class="WHIT">	  </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">"');}return p.join('');"</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 29</span> </span><span class="WHIT">		</span><span class="NAME">fnStr</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">fnStr.replace</span><span class="PUNC">(</span><span class="REGX">/@SPC@/g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">"\\"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 30</span> </span><span class="WHIT">		</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">fn</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">Function</span><span class="PUNC">(</span><span class="STRN">"obj"</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">fnStr</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 31</span> </span><span class="WHIT">		</span><span class="NAME">tmpl.cache</span><span class="PUNC">[</span><span class="NAME">tmpl_id</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">fn</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 32</span> </span><span class="WHIT">		</span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">fn</span><span class="PUNC">(</span><span class="NAME">data</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 33</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">catch</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">e</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 34</span> </span><span class="WHIT">		</span><span class="NAME">log</span><span class="PUNC">(</span><span class="NAME">e</span><span class="PUNC">+</span><span class="STRN">":"</span><span class="PUNC">+</span><span class="NAME">tmpl_id</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">'rendering'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 35</span> </span><span class="WHIT">		</span><span class="KEYW">return</span><span class="WHIT"> </span><span class="STRN">""</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 36</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 37</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 38</span> </span></pre></body></html>